<?php
namespace app\SuperAdmin\controller;

use app\SuperAdmin\model\ActivitiesModel;
use think\Controller;
use think\Session;
use think\Db;
class ActivitiesController extends Controller
{
    public function _initialize()
    {
        if (!Session::has('adminid'))
        {
            $this->redirect('index/index/index');
        }
    }

    public function index()
    {
        $result=null;
        if(input('keyword'))
        {
            $result=db('activities')->where('aname','like','%'.input('keyword').'%')->order('aflag desc,aaddtime desc')->paginate(8)
                ->each(function($item){
                    $num=\db('enroll')->where('aid',$item['aid'])->count();
                    $item['num']=$num;
                    return $item;
                });
        }
        else
        {
            $result=db('activities')->order('aflag desc,aaddtime desc')->paginate(8)
                ->each(function($item){
                    $num=\db('enroll')->where('aid',$item['aid'])->count();
                    $item['num']=$num;
                    return $item;
                });
        }
        $this->assign('list',$result);
        return $this->fetch('activities');
    }

    public function add()
    {
        $message=$_POST;
        $message['aaddtime']=date("Y-m-d H:i");
        $file=request()->file('apic');
        $info_img=$file->rule('uniqid')->validate(['ext'=>'jpg,png'])->move(ROOT_PATH.'public/static/img/');
        if($info_img)
        {
            $message['apic']=$info_img->getFilename();
        }
        else
        {
            $this->error($file->getError());
        }
        $activities=new ActivitiesModel();
        $activities->data($message);
        $activities->save();
        $this->success('添加成功');
        die();
    }

    public function detail_json($id)
    {
        echo json_encode(\db('activities')->find($id));
        die();
    }

    public function setFlag($aid)
    {
        $activities=ActivitiesModel::get($aid);
        $flag=$activities['aflag'];
        if($flag==0)
        {
            $model=new ActivitiesModel();
            $num=$model->where('aflag',1)->count();
            if($num>=5)
            {
                $this->error('推荐位已达上限');
            }
        }
        $flag=($flag+1)%2;
        ActivitiesModel::update(['aid'=>$aid,'aflag'=>$flag]);
        $this->redirect('index');
    }

    public function update()
    {
        $message=$_POST;
        if($message['astarttime']=='0000-00-00 00:00:00' || $message['astarttime']=='')
        {
            $result=\db('activities')->find($message['aid']);
            if($result)
            {
                $message['astarttime']=$result['astarttime'];
            }
        }
        $file=request()->file('apic');
        if($file)
        {
            $info_img=$file->rule('uniqid')->validate(['ext'=>'jpg,png'])->move(ROOT_PATH.'public/static/img/');
            if($info_img)
            {
                $result=\db('activities')->field('apic')->find($message['aid']);
                if($result)
                {
                    if(file_exists(ROOT_PATH.'public/static/img/'.$result['apic']))
                    {
                        unlink(ROOT_PATH.'public/static/img/'.$result['apic']);
                    }
                }
                $message['apic']=$info_img->getFilename();
            }
            else
            {
                $this->error($file->getError());
            }
        }
        try{
            \db('activities')->update($message);
        }
        catch (\mysqli_sql_exception $exception)
        {
            $this->error('修改失败');
        }
        $this->success('修改成功');
    }

    public function delete($aid)
    {
        $activity=ActivitiesModel::get($aid);
        if(file_exists(ROOT_PATH.'public/static/img/'.$activity->apic))
        unlink(ROOT_PATH.'public/static/img/'.$activity->apic);
        $activity->delete();
        $this->success('删除成功');
    }
}